<template>
	<Layout>
        <Table ref='table' :height="tableHeight"  no-data-text="没有查询到相关数据!" :loading="loading"
                border :columns="columns1"  :data="dataTable">
        </Table>

        <Row style="margin-right:10px;">
            <Col span="24" >
                    <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
            </Col>
        </Row>
        <Modal :transfer=false footer-hide v-model="model1"  :width=900>
            <Table ref='table' :height="tableHeight"  no-data-text="没有查询到相关数据!"
                   border :columns="columns2"  :data="history">
            </Table>
        </Modal>
        <Modal title="流程图" :transfer=false footer-hide v-model="model2"  :width=900>
            <img :src="imgUrl" style="width: 100%; min-height:400px;">
        </Modal>
	</layout>
</template>

<script>
	import {Api} from '../../../api/index.js'
    import { sessionStorage, cookie } from '../../../common/cacheUtils.js';
	export default {
        components: {
        },
		data() {
			return {
                loading: false,
                imgUrl: '',
				form1: {},
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
				columns1: [
				    {
                        type: 'index',
                        title: '序号',
                        minWidth: 80,
                        align: 'center',
					 },
					{
                        minWidth: 250,
                        title: '流程名称',
                        align: 'center',
                        key: 'procinstName',
					},
					{
                        minWidth: 150,
						title: '流程状态',
						key: 'procinstStateName',
						align: 'center',
					},
					{
                        minWidth: 210,
						title: '上报时间',
						key: 'createDate',
                        align: 'center',
                        render:(h,params)=>{
                            return h('div',[
                                (this.formatDate(new Date(params.row.createDate), 'yyyy-MM-dd'))
                            ]);
                        }
					},
					{
                        minWidth: 210,
						title: '上报人',
						key: 'userName',
						align: 'center'
					},
					{
                        minWidth: 200,
						title: '当前任务节点名称',
						key: 'taskName',
						align: 'center'
					},
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        minWidth: 250,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: "12px"
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params.row);
                                        }
                                    }
                                }, '详情'),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: "12px",
                                        marginLeft: '8px'
                                    },
                                    on: {
                                        click: () => {
                                             this.showHistory(params.row);
                                        }
                                    }
                                },'审批记录'),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: "12px",
                                        marginLeft: '8px'
                                    },
                                    on: {
                                        click: () => {
                                            this.imgUrl = Api.getFlowImg + '/' + params.row.procinstId
                                            this.model2 = true;
                                        }
                                    }
                                },'流程图')
                            ]);
                        }
                    }
				],
                dataTable: [],
                tableHeight : 10,
                model1 : false,
                model2 : false,
                history : [],
                columns2: [
                    {
                        type: 'index',
                        title: '序号',
                        width: 80,
                        align: 'center',
                    },
                    {
                        width: 180,
                        title: '审批时间',
                        key: 'endTime',
                        align: 'center',
                    },
                    {
                        width: 185,
                        title: '节点名称',
                        key: 'name',
                        align: 'center',
                    },
                    {
                        width: 120,
                        title: '审批人',
                        key: 'userName',
                        align: 'center'
                    },
                    {
                        width: 100,
                        title: '审批意见',
                        key: 'route',
                        align: 'center'
                    },
                    {
                        width: 200,
                        title: '备注',
                        key: 'opinions',
                        align: 'center'
                    },
                ],
			}
		},
		mounted: function() {
            this.tableHeight = window.innerHeight - 160;
			this.query();
        },
		methods: {
            pageChange(pageNum) {
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            pageSizeChange(pageSize) {
                this.pagedata.pageSize = pageSize;
                this.query();
            },

            showData(row) {
                let formKey = row.formKey;
                //测试
                formKey = 'ptwSchemeForApproval';

                this.$router.push({ name: formKey, params: { 'id': row.workId, taskId : row.taskId, viewFlag : true } });
            },

            showHistory(row) {
                this.model1 = true;

                this.$http.get(Api.getHistory + '/' + row.procinstId, {params: {}}).then((res) => {
                    this.history = res.data;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },

            query() {
                let user = sessionStorage('userId').get('userId');
                let userJson = JSON.parse(user);

                let data = {
                    currentPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize,
                    // createUserId :userJson.userId,
                }
                this.loading = true;
                let url = Api.getDoneList;
                this.$http.get(url, {params: data}).then((res) => {
                    this.dataTable = res.data.data;
                    this.dataTable.map(item=>{
                        if(item.procinstState =='0' || item.procinstState ==null){
                            item.procinstStateName = '未上报';
                        }else if(item.procinstState =='1'){
                            item.procinstStateName = '已上报';
                        }else if(item.procinstState =='2'){
                            item.procinstStateName = '被退回';
                        }else if(item.procinstState =='3'){
                            item.procinstStateName = '已审批';
                        }
                    })
                    this.pagedata.total = res.data.totalSize;
                    this.loading = false;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },

        },
	}
</script>
<style lang='css' scoped>
    @import '../../index.css';

</style>
